#!/bin/bash
#
# Copyright (C) 2012, Luis R. Rodriguez <mcgrof@do-not-panic.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# You can use this script to install all mainline kernels used
# to test compile the Linux kernel compatibility module. You can
# then use ckmake to cross compile against all supported kernels.

# Pretty colors
GREEN="\033[01;32m"
YELLOW="\033[01;33m"
NORMAL="\033[00m"
BLUE="\033[34m"
RED="\033[31m"
PURPLE="\033[35m"
CYAN="\033[36m"
UNDERLINE="\033[02m"

KERNELS=""
KPATH="http://kernel.ubuntu.com/~kernel-ppa/mainline/"

FORCE="0"
KSRC_PREFIX=
# Check whether we're running as root or not
# If we're root stuff things into /usr/src and /lib/modules
# else, use $HOME/compat-ksrc for enabling user-mode builds.
if [[ "$EUID" != "0" ]]; then
	KSRC_PREFIX="$HOME/compat-ksrc"
else
	KSRC_PREFIX="${PWD}/compat-ksrc"
fi


# Create target directories if they doesn't exist
mkdir -p $KSRC_PREFIX/{usr/src,lib/modules}

# List of currently supported kernels that will be downloaded
KERNELS="$KERNELS ${KPATH}/v2.6.24/linux-headers-2.6.24-020624_2.6.24-020624_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.25/linux-headers-2.6.25-020625_2.6.25-020625_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.26/linux-headers-2.6.26-020626_2.6.26-020626_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.27/linux-headers-2.6.27-020627_2.6.27-020627_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.28.10/linux-headers-2.6.28-02062810_2.6.28-02062810_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.29.6/linux-headers-2.6.29-02062906_2.6.29-02062906_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.30.10/linux-headers-2.6.30-02063010_2.6.30-02063010_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.31.13-karmic/linux-headers-2.6.31-02063113_2.6.31-02063113_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.32.60-lucid/linux-headers-2.6.32-02063260_2.6.32-02063260.201210082135_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.33.20-maverick/linux-headers-2.6.33-02063320_2.6.33-02063320.201111071735_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.34.14-maverick/linux-headers-2.6.34-02063414_2.6.34-02063414.201301162135_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.35.13-original-maverick/linux-headers-2.6.35-02063513_2.6.35-02063513.201107261012_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.36.4-natty/linux-headers-2.6.36-02063604_2.6.36-02063604.201102180911_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.37.6-natty/linux-headers-2.6.37-02063706_2.6.37-02063706.201103281005_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.38.8-natty/linux-headers-2.6.38-02063808_2.6.38-02063808.201106040910_all.deb"
KERNELS="$KERNELS ${KPATH}/v2.6.39.4-oneiric/linux-headers-2.6.39-02063904_2.6.39-02063904.201108040905_all.deb"
KERNELS="$KERNELS ${KPATH}/v3.0.76-oneiric/linux-headers-3.0.76-030076_3.0.76-030076.201305011235_all.deb"
KERNELS="$KERNELS ${KPATH}/v3.1.10-precise/linux-headers-3.1.10-030110_3.1.10-030110.201201181135_all.deb"
KERNELS="$KERNELS ${KPATH}/v3.2.44-precise/linux-headers-3.2.44-030244_3.2.44-030244.201304260219_all.deb"
KERNELS="$KERNELS ${KPATH}/v3.3.8-quantal/linux-headers-3.3.8-030308_3.3.8-030308.201206041356_all.deb"
KERNELS="$KERNELS ${KPATH}/v3.4.43-quantal/linux-headers-3.4.43-030443_3.4.43-030443.201305011335_all.deb"
KERNELS="$KERNELS ${KPATH}/v3.5.7.11-quantal/linux-headers-3.5.7-03050711_3.5.7-03050711.201304220635_all.deb"
KERNELS="$KERNELS ${KPATH}/v3.6.11-raring/linux-headers-3.6.11-030611_3.6.11-030611.201212171335_all.deb"
KERNELS="$KERNELS ${KPATH}/v3.7.10-raring/linux-headers-3.7.10-030710_3.7.10-030710.201302271235_all.deb"
KERNELS="$KERNELS ${KPATH}/v3.8.11-raring/linux-headers-3.8.11-030811_3.8.11-030811.201305011408_all.deb"
KERNELS="$KERNELS ${KPATH}/v3.9-raring/linux-headers-3.9.0-030900_3.9.0-030900.201304291257_all.deb"

# Number of kernels
NUM_KERNELS=$(echo $KERNELS | wc -w)

# ~ 101 MiB for installed space on $KSRC_PREFIX/usr/src/ and $KSRC_PREFIX/lib/modules/
SPACE_PER_KERNEL="101"

# ~13 MiB of both deb files
SPACE_PER_KERNEL_DEB="13"

function get_ubuntu_kernels() {

	ARCH=$(uname -m)
	TARGET=""

	case $ARCH in
	"x86_64")
		TARGET="amd64"
		;;
	"i686")
		TARGET="i386"
		;;
	*)
		echo -e "Unsupported architecture"
		exit
		;;
	esac

	mkdir -p debs
	cd debs

	for i in $KERNELS; do
		FILE=$(basename $i)
		PKG=$(echo $FILE | awk -F"_" '{print $1}')

		# Do not download if installed or deb exists
		if [[ ! -d $KSRC_PREFIX/usr/src/$PKG && ! -f $FILE ]]; then
			# Download the _all.deb
			wget -c $i

			# Download the generic-ARCH headers
			GENERIC_DEB=`echo $i | sed -e "s:\(.*\)_\(.*\)_all.deb:\1-generic_\2_$TARGET.deb:"`
			wget -c $GENERIC_DEB
		fi
	done

	# List of downloaded debs
	DEB_LIST=`ls linux-*.deb 2>/dev/null`

	if [[ -z $DEB_LIST ]]; then
		echo "All kernel sources are found in $KSRC_PREFIX/usr/src."
		exit
	fi

	# Create a temporary directory first
	TEMP_DIR=`mktemp -d`

	for deb in $DEB_LIST; do
		DIR_NAME=$(echo $deb | awk -F"_" '{print $1}')
		if [[ ! -d $KSRC_PREFIX/usr/src/$DIR_NAME ]]; then
			echo "Extracting $deb..."
			ar p $deb data.tar.gz | tar xz --exclude=usr/share -C $TEMP_DIR
		fi
	done

	# Move the extracted folders into the system
	if [[ -d $TEMP_DIR/lib/modules ]]; then

		# Relink lib/modules/*/build/ directories relatively to make it work
		# for regular user based workflows
		if [[ -n $KSRC_PREFIX ]]; then
			echo "Adjusting build/ symlinks for non-root installation..."

			for kernel in $(ls -d $TEMP_DIR/lib/modules/*generic); do
				unlink $kernel/build
				ln -s ../../../usr/src/linux-headers-`basename $kernel` $kernel/build
			done
		fi

		mv $TEMP_DIR/lib/modules/* $KSRC_PREFIX/lib/modules
	fi
	if [[ -d $TEMP_DIR/usr/src ]]; then
		# Because of a bug in make < 3.82, mixed implicit and normal
		# rules do not cause harm. Since the bug is fixed in the new make
		# we have to adjust older kernel's Makefiles to fix the bug.
		sed -i 's#^/ %/:#%/:#' $TEMP_DIR/usr/src/linux-headers-2.6.2[45678]-0*/Makefile &>/dev/null

		mv $TEMP_DIR/usr/src/* $KSRC_PREFIX/usr/src
	fi

	# Remove the temporary directory
	rm -rf $TEMP_DIR
}

function rebuild_header_binary_deps() {
	if [[ ! -d ${KSRC_PREFIX}/lib/modules/ ]]; then
		echo "You do not seem to have any vanilla kernels available to fix"
		exit 1
	fi

	COUNT=$(ls -d ${KSRC_PREFIX}/lib/modules/*generic | wc -l)
	if [[ $COUNT -le 0 ]]; then
		echo "You do not seem to have any vanilla kernels available to fix"
		exit 1
	fi

	for kernel in $(ls -d ${KSRC_PREFIX}/lib/modules/*generic | grep -E "/3.[2-9]|/[4-9]"); do
		echo $kernel

		count=0
		while [[ $count -ne 4 ]]; do
			for i in basic mod genksyms; do
				if [[ $count -eq 0 ]]; then
					make -C ${kernel}/build/ M=scripts/${i}/ clean > /dev/null 2>&1
				fi
				make -C ${kernel}/build/ M=scripts/${i}/ > /dev/null 2>&1
			done
			let count=$count+1
		done
	done
}

function usage() {
	echo -e "Usage: $0 [ -r ] [ -f ] "
	echo -e "-r Rebuilds binaries required in kernel headers. Use"
	echo -e "   this option if you've already ran this script and"
	echo -e "   just need to fix the libc versions against which"
	echo -e "   the binaries in the headers files are linked against. "
	echo -e "   This was added since kernels >= 3.4 require"
	echo -e "   a glibc >= 2.14 for memcpy(), and not all Linux"
	echo -e "   distributions have such versions of glibc."
	echo -e ""
	echo -e "-f Force running, do not ask"
}

if [[ $# -gt 3 ]]; then
	usage
	exit 1
fi

if [[ $1 == "-r" ]]; then
	rebuild_header_binary_deps
	exit
fi

if [[ $1 == "-f" ]]; then
	FORCE="1"
	shift
fi

# Check for the availability of 'ar' before continuing
which ar 2>&1 > /dev/null
if [[ $? -ne 0 ]]; then
	echo -e "${GREEN}ar${NORMAL} is not avilable, typically this is available through a package called binutils"
	echo -e "Install binutils and run this script again..."
	exit 1
fi

echo -e ""

if [[ ! -n $KSRC_PREFIX ]]; then
	echo -e "** Running as a privileged user!"
	echo -e "** You are trying to force using ${BLUE}${KSRC_PREFIX}/lib/modules${NORMAL} and ${BLUE}${KSRC_PREFIX}/usr/src${NORMAL} ..."
	echo -e "** This is a terrible idea. Consider running as a regular user."
	echo -e ""
	read -p "Do you still want to continue (y/N)? "
	if [[ "${REPLY}" != "y" ]]; then
		echo -e "Bailing out !"
		exit 1
	fi
fi

echo -e "This will download ${YELLOW}${NUM_KERNELS}${NORMAL} kernel headers to allow you to"
echo -e "cross compile any module over these kernels with ${GREEN}ckmake${NORMAL}."
echo -e "The download payload is about ${YELLOW}~ $((${SPACE_PER_KERNEL_DEB} * ${NUM_KERNELS})) ${CYAN}MiB${NORMAL}, once uncompressed"
echo -e "it will stash kernel header files under ${BLUE}$KSRC_PREFIX/usr/src/${NORMAL}"
echo -e "and ${BLUE}$KSRC_PREFIX/lib/modules/${NORMAL} and consume about ~ ${YELLOW}$((${NUM_KERNELS} * ${SPACE_PER_KERNEL})) ${RED}MiB${NORMAL} of space."
echo -e ""
echo -e "The kernel headers used are from ${PURPLE}${UNDERLINE}Vanilla${NORMAL} kernels"
echo -e "from the Ubuntu mainline / vanilla kernel PPA and are extracted"
echo -e "using ${GREEN}ar${NORMAL} and ${GREEN}tar${NORMAL}:"
echo -e ""
echo -e "${BLUE}http://kernel.ubuntu.com/~kernel-ppa/mainline/${NORMAL}"
echo -e ""

if [[ "$FORCE" != "1" ]]; then
	read -p "Do you still want to continue (y/N)? "
	if [[ "${REPLY}" != "y" ]]; then
		echo -e "Bailing out !"
		exit 1
	fi
fi

get_ubuntu_kernels
echo -e "Vanilla kernels headers ${BLUE}installed${NORMAL}, "
echo -e "now going to ${GREEN}rebuild${NORMAL} some binary dependencies..."
sleep 1
rebuild_header_binary_deps
